<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .head {
            height: 1000px;
        }

        .content {
            margin: 0 auto;
            text-align: center;
            height:400px;
        }

        .content img {
            width: 400px;
        }
    </style>
</head>

<body>
    <div class="head">123</div>
    <div class="content"><img src="" alt="" datasrc="img/bgc.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/bgc1.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/bgc2.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/bgc3.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/bgc4.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/bgc5.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/gaoji.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/haha.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/yz.jpg"></div>
    <div class="content"><img src="" alt="" datasrc="img/头像.jpg"></div>
    <script>
        //获取所有图片节点
        var imgs = document.getElementsByTagName('img')

        //获取可视区域高度
        var viewHeight = window.innerHeight
        // 当前显示到了哪张图片
        var num = 0

        function lazyload() {
            console.log('scrolling....');
            for (let i = num; i < imgs.length; i++) {
                if (imgs[i].getBoundingClientRect().top <= viewHeight) {
                    console.log('###'+i);
                    imgs[i].src = imgs[i].getAttribute('datasrc')
                    num = i + 1
                }
            }
        }

        function debounce(fn,delay){
            let timer = null
            return function(){
                if(timer){
                    clearTimeout(timer)
                }
                timer = setTimeout(function(){
                    fn()
                },delay)
            }
        }
        window.addEventListener('scroll',debounce(lazyload,600))
        window.onload = lazyload;
    </script>
</body>

</html>